package com.shanjupay.merchant.service;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.common.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class FileServiceImpl implements FileService {
        @Value("${oss.qiniu.url}")
        private String qiniuUrl;
        @Value("${oss.qiniu.accessKey}")
        private String accessKey;
        @Value("${oss.qiniu.secretKey}")
        private String secretKey;
        @Value("${oss.qiniu.bucket}")
        private String bucket;
    /**
     *  上传文件
     * @param bytes 文件的字节数组
     * @param fileName 文件的名称
     * @return 文件的访问路径 （绝对url）
     * @throws BusinessException
     */
    @Override
    public String upload(byte[] bytes, String fileName) throws BusinessException {
        //调用common下面的工具类
        try {
            //String accessKey,String secretKey,String bucket,byte[] bytes,String fileName
            QiniuUtils.upload2qiniu(accessKey,secretKey,bucket,bytes,fileName);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new BusinessException(CommonErrorCode.E_100106);
        }
        //上传成功后返回图片的访问路径（绝对路径）
        return qiniuUrl+fileName;

    }
}
